jQuery(document).ready(function(){
	shop.header.init();
});
shop.header = {
	conf:{city:''},
	init:function(){
		shop.header.menuTop();
		shop.header.facebook();
		shop.header.email.start();
	},
	focusPass:{
		focus:function(obj){
			var dom = document.createElement('input');
			dom.setAttribute('type', 'password');
			dom.setAttribute('name', 'customer_password');
			dom.className = 'inputPassword';
			dom.id = 'customer_password';
			dom.onblur = function(){
				shop.header.focusPass.blur(this);
			};
			var p = jQuery(obj).parent();
			p.children().remove();
			p.append(dom);
			dom.focus();
		},
		blur:function(obj){
			if(obj.value == ''){
				jQuery(obj).parent().html('<input type="text" name="customer_password" id="customer_password" class="inputPassword" title="Mật khẩu" value="Mật khẩu" onfocus="shop.header.focusPass.focus(this)" />');
			}
		}
	},
	menuTop: function(){
		jQuery('.submenu').hover(
		  function(){
			var offset = jQuery(this).offset(),
			w = jQuery('ul.menu', this).width();
			if(offset.left + w > jQuery(document).width()){
				jQuery('ul.menu', this).css('left', (jQuery(this).width() - w)+'px' );
			}else{
				jQuery('ul.menu', this).css('left', 'auto');
			}
		  },
		  function(){
			jQuery('ul.menu', this).css('left', '-999em');
		  }
		);
	},
	facebook: function() {
		function iAction()
		{
			jQuery('#fb_container').removeClass("hidden");
		}

		//Kiem tra trinh duyet
		if (jQuery.browser.safari || jQuery.browser.opera)
		{
			jQuery('#fb_iframe').load(function(){
				setTimeout(iAction, 0);
			});
			//Can load lai
			var iSource = jQuery('#fb_iframe').attr("src");
			jQuery('#fb_iframe').attr("src", "");
			jQuery('#fb_iframe').attr("src", iSource);
		}
		else
		{
			//Voi browser ho tro
			jQuery('#fb_iframe').load(function(){
				iAction();
			});
		}
    },
	city:{
		start:function(){
			var city = shop.cookie.get('city');
			if(city == ''){
				shop.show_overlay_popup('popcity', '', shop.header.theme.popupCity(),
				{
					background: {'background-color' : 'transparent'},
					border: {
						'background-color' : 'transparent',
						'padding' : '0px'
					},
					title: {'display' : 'none'},
					content: {
						'padding' : '0px',
						'width' : '534PX'
					},
					release:function(){
						shop.fix_png('.popupTQ_bg');
					}
				});
			}
		},
		menuCity:function(obj, over){
			if(over){
				jQuery('.current',obj).addClass('over');
				jQuery('.sublistCity').removeClass('hidden')
			}else{
				jQuery('.current',obj).removeClass('over');
				jQuery('.sublistCity').addClass('hidden')
			}
		},
		setCity:function(id,safe_title){
			shop.cookie.set('city', id, 86400*7, '/');
			//if(query_string.indexOf('?page') > -1){
			//	window.location.reload();
			//}
			//else{
				window.location.href = BASE_URL+safe_title;
			//}
		}
	},
	email:{
		start:function(){
			jQuery('body').prepend(shop.header.theme.htmlRegMail());
			jQuery('#RegEmail').jNice();
			if(shop.cookie.get('showRegEmail') != ''){
			  jQuery('#formRegMail').hide();
			}
		},
		hideFormEmail:function() {
			var form = '#formRegMail';
			jQuery(form).slideToggle('slow',function () {
				if(jQuery(form).is(':hidden') == true){
					shop.cookie.set('showRegEmail', 'not', 86400*365, '/');
				}
			});
		},
		registerEmail: function(id){
			id = id ? id : 'sub-email';
			var email = shop.get_ele(id),
			city = jQuery('#regEmailListProvince').val();
			if(email){
				if(shop.util_trim(email.value) == ''){
					alert('Chưa nhập Email');
					email.select();
					email.focus();
					return;
				}else if(!shop.is_email(email.value)){
					alert('Email không hợp lệ');
					email.select();
					email.focus();
					return;
				}else{
					shop.ajax_popup('act=cart&code=register-email',"POST",{email: email.value,city:city},
					function (j) {
						shop.hide_loading();
						if (j.err == 0) {
							shop.cookie.set('showRegEmail', 'not', 86400*365, '/');
							jQuery('#formRegMail').slideUp('slow');
							if(shop.is_exists(city_list[city])){
								city = city_list[city].title;
							}else{
								city = 'Toàn Quốc';
							}
							alert('Quý khách đã hoàn thành đăng kí nhận email thông báo giảm giá hàng ngày phạm vi '+city);
							email.value = '';
						} else {
							alert(j.msg);
							email.select();
							email.focus();
						}
					});
				}
			}
		}
	},

	theme:{
		popupCity:function(){
			return shop.join
			('<div id="popupTQ" class="popupCity">')
				('<div class="popupTQ_bg"></div>')
				('<div class="popupTQ_c">')
					('<div><a href="javascript:void(0);" title="Đóng cửa sổ" class="choiceClose" onclick="shop.header.city.setCity(0,\'\');"></a><div class="c"></div></div>')
					('<div class="buttonCity">')
						('<a id="hn" title="Hà Nội" href="javascript:void(0);" onclick="shop.header.city.setCity(22,\'ha-noi\')"></a>')
						('<a id="tphcm" title="Hồ Chí Minh" href="javascript:void(0);" onclick="shop.header.city.setCity(29,\'tp-ho-chi-minh\')"></a>')
						('<a id="dn" title="Đà Nẵng" href="javascript:void(0);" onclick="shop.header.city.setCity(15,\'da-nang\')"></a>')
						('<div class="c"></div>')
					('</div>')
				('</div>')
			('</div>')();
		},
		htmlRegMail:function(){
			return shop.join
			('<div id="formRegMail">')
			  ('<div class="regContent">')
				('<div class="text fl"><b>Đăng ký nhận email thông báo giảm giá tại</b></div>')
				('<div class="fl mLeft20" id="RegEmail">')
				  ('<select style="width:133px" id="regEmailListProvince">'+shop.header.conf.city+'</select>')
				('</div>')
				('<div class="bgSubscribeEmail fl">')
				  ('<input type="text" name="email" id="sub-email" value="Nhập email của bạn" onfocus="javascript:if(this.value == \'Nhập email của bạn\') this.value=\'\';" onblur="javascript:if(this.value == \'\')this.value=\'Nhập email của bạn\';"/>')
				('</div>')
				('<a href="javascript:void(0)" onclick="shop.header.email.registerEmail()" class="bt_send_email"></a>')
				('<a href="javascript:void(0)" onclick="shop.header.email.hideFormEmail()" class="bt_hide_email">Ẩn [ x ]</a>')
				('<div class="leftBorderReg"></div>')
				('<div class="rightBorderReg"></div>')
			  ('</div>')
			('</div>')();
		}
	}
};
